<template>
	<el-dialog title="" width="70%" visible :show-close="false" :append-to-body="true" class="wrap">
		<i class="el-icon-close close" @click="cancel" style="color: white;"></i>
		<div class="bt">{{vtitle}}</div>
		<el-form label-width="160px" inline class="form">

			<el-form-item label="老师名称">
				<el-select v-model="form.lsid">
					<el-option :label="v.mc" :value="v.id" v-for="v in comboxlist"></el-option>
				</el-select>
				<el-button type="" size="mini" @click="lsjc()">查看老师空闲时间</el-button>
				<el-button type="" size="mini" @click="jsjc()">查看教室空闲时间</el-button>
			</el-form-item>
			<el-form-item label="从指定Day开始排课">
				<el-select clearable v-model="vday" class="code-search" placeholder="请选择day">
					<el-option :label="v.daymc" :value="v.day" v-for="v in comboxlistday"></el-option>
				</el-select>
				<el-button type="success" size="mini" style="" @click="bccl('Y')">保存</el-button>
				<el-button type="" size="mini" style="" @click="bccl('N')">查看冲突</el-button>
				<el-checkbox v-model="iscx">是否检查冲突</el-checkbox>
			</el-form-item>
			<hr/>
			<ul>
				<li class="lis" v-for="v in cllist">
					<span class="sp1">{{v.weektext}}</span>
					<span class="sp1" style="width:100px ;">{{v.bjtime}}</span>
					<span class="sp1">{{v.jsmc}}</span>
					<!--<span class="sp1">{{v.sjd}}</span>-->
					<span style="float: left; width: 60%; text-align: center;">
						<el-select v-model="v.sjd1" style="width: 20%;">
							<el-option v-for=" v in hourlist " :label="v" :value="v"></el-option>
						</el-select>
						<el-select v-model="v.sjd2" style="width: 20%;">
							<el-option v-for=" v in minslist " :label="v" :value="v"></el-option>
						</el-select>
						<span class="a" style=""> &nbsp;&nbsp;--&nbsp;&nbsp; </span>
						<el-select v-model="v.sjd3" style="width: 20%;">
							<el-option v-for=" v in hourlist " :label="v" :value="v"></el-option>
						</el-select>
						<el-select v-model="v.sjd4" style="width: 20%;">
							<el-option v-for=" v in minslist " :label="v" :value="v"></el-option>
						</el-select>
					</span>
					
					<span style="width: 10%; text-align: center; float: right;" @click="removecl(v.id)">移除</span>
				</li>
			</ul>
			<hr/>
			<div class="ullist">
				<el-button @click="setbtnday(0)" size='small'>今天</el-button>
				<el-button @click="setbtnday(-7)" size='small'>&lt;&lt;</el-button>
				<el-radio-group v-model="but" size='small' @change="getsxsjdlist()">
					<el-radio-button :label="1">周一({{butday1}})</el-radio-button>
					<el-radio-button :label="2">周二({{butday2}})</el-radio-button>
					<el-radio-button :label="3">周三({{butday3}})</el-radio-button>
					<el-radio-button :label="4">周四({{butday4}})</el-radio-button>
					<el-radio-button :label="5">周五({{butday5}})</el-radio-button>
					<el-radio-button :label="6">周六({{butday6}})</el-radio-button>
					<el-radio-button :label="7">周日({{butday7}})</el-radio-button>
				</el-radio-group>
				<el-button @click="setbtnday(7)" size='small'>&gt;&gt; </el-button>
			</div>

			<table class="tab">
				<tr>
					<td>教室</td>
					<td>上午</td>
					<td>下午</td>
					<td>夜晚</td>
				</tr>

				<tr style="height:100px" v-for='v in jssjdlist'>
					<td style="width:14.22%;">{{v.jsmc}}</td>
					<td style="width:20%;">
						<div :style="v.dis1=='Y' ? 'background:#eee;':''" >
							<el-checkbox v-model="v.chk1" :label="v.sjd1" v-if="v.sjd1" border @change="addcl()"></el-checkbox>
						</div>
						<div :style="v.dis2=='Y' ? 'background:#eee':''">
							<el-checkbox v-model="v.chk2" :label="v.sjd2" v-if="v.sjd2" border @change="addcl()"></el-checkbox>
						</div>
					</td>
					<td style="width:20%;">
						<div :style="v.dis3=='Y' ? 'background:#eee':''">
							<el-checkbox v-model="v.chk3" :label="v.sjd3" v-if="v.sjd3" border @change="addcl()"></el-checkbox>
						</div>
						<div :style="v.dis4=='Y' ? 'background:#eee':''">
							<el-checkbox v-model="v.chk4" :label="v.sjd4" v-if="v.sjd4" border @change="addcl()"></el-checkbox>
						</div>
					</td>
					<td style="width:20%;">
						<div :style="v.dis5=='Y' ? 'background:#eee':''">
							<el-checkbox v-model="v.chk5" :label="v.sjd5" v-if="v.sjd5" border @change="addcl()"></el-checkbox>
						</div>
						<div :style="v.dis6=='Y' ? 'background:#eee':''">
							<el-checkbox v-model="v.chk6" :label="v.sjd6" v-if="v.sjd6" border @change="addcl()"></el-checkbox>
						</div>
					</td>
				</tr>
			</table>

			<Jcct @close="clolsjc" :params="this.params" v-if="showjcctlist"></Jcct>
			<Lsjc @close="clolsjc" :params="this.params" v-if="showlsjclist"></Lsjc>
			<Jsjc @close="clolsjc" :params="this.params" v-if="showjsjclist"></Jsjc>
		</el-form>

	</el-dialog>
</template>
<script type="text/javascript">
	import qs from 'qs'
	import Lsjc from './lsjc'
	import Jsjc from './jsjc'
	import Jcct from './jcct'
	export default {
		props: ['params'],
		components: {
			Lsjc,Jsjc,Jcct
		},
		data() {
			return {
				iscx:false,
				vtitle: '重置课表',
				btnshowkb: false,
				vday: '',
				jssjdlist: [],
				cllist: [],
				vkcid: '',
				xzlsList: [],
				comboxlist: [],
				xsdataList: [],
				comboxlistday: [],
				form: {
					lsid: '',
				},
				hourlist: ['', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22'],
				minslist: ['', '00', '05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55'],
				but: '',
				butday1: '',
				butday2: '',
				butday3: '',
				butday4: '',
				butday5: '',
				butday6: '',
				butday7: '',
				butvallist: [],
				sjdboxlist: [],
				showlsjclist: false,
				showjsjclist: false,
				showjcctlist:false,
				showchoselist: false,
				dialogTableVisible: false,
				showarticle: false,
				showbjxs: false,
				showjskblist: false,
				showlskblist: false,
				showjslskblist: false,
				wek: 0,
				// 分页

				/*组件区域*/
				portComponents: {

				},

			}
		},

		created() {
			this.form.lsid = this.params.lsid
			this.getcomlist2()
			this.setbtnday(0);
			this.getcomboxlist(); //laoshi

		},

		methods: {
			getcomlist2() {
				var logininfo = JSON.parse(localStorage.logininfo);
				var params2 = qs.stringify({
					yhid: logininfo.id,
					xxid: logininfo.xxid,
					bjid: this.params.id, // 班级id
				})
				//执行
				this.axios.post(this.$store.state.api + 'bj/getpkday', params2)
					.then(res => {
						let data = res.data.data;
						let list = data.list;
						this.comboxlistday = list;
						this.vday = this.comboxlistday[0].day
					}).catch(res => {})
			},
			ckct(){
				this.showjcctlist = true
			},
			lsjc() {
				this.showlsjclist = true
			},
			jsjc() {
				this.showjsjclist = true
			},
			clolsjc() {
				this.showlsjclist = false
				this.showjsjclist = false
				this.showjcctlist = false
			},
			addDate(date, days) {
				var d = new Date(date);
				d.setDate(d.getDate() + days);
				return d
			},
			getFormatDatemmdd(date) {
				var seperator1 = "-";
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var strDate = date.getDate();
				if(month >= 1 && month <= 9) {
					month = "0" + month;
				}
				if(strDate >= 0 && strDate <= 9) {
					strDate = "0" + strDate;
				}
				// var currentdate = year + seperator1 + month + seperator1 + strDate;
				var currentdate = month + seperator1 + strDate;
				return currentdate;
			},
			getFormatDate(date) {
				var seperator1 = "-";
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var strDate = date.getDate();
				if(month >= 1 && month <= 9) {
					month = "0" + month;
				}
				if(strDate >= 0 && strDate <= 9) {
					strDate = "0" + strDate;
				}
				var currentdate = year + seperator1 + month + seperator1 + strDate;
				return currentdate;
			},
			setbtnday(wekstep) {
				if(wekstep == 0) {
				    //获取当前星期几
				    var d1 = this.addDate(new Date(), 0);
				    var tw =d1.getDay();
				    if (tw==0) tw = 7;
					this.but=tw;
					
					
					this.wek = 0;
				} else {
					this.wek = this.wek + wekstep;
				}
				this.butvallist = [];
				var d = this.addDate(new Date(), this.wek);
				var wk = d.getDay();
				var dd = this.addDate(d, (1 - wk));
				this.butday1 = this.getFormatDatemmdd(dd);
				this.butvallist.push(this.getFormatDate(dd));
				dd = this.addDate(d, (2 - wk));
				this.butday2 = this.getFormatDatemmdd(dd);
				this.butvallist.push(this.getFormatDate(dd));
				dd = this.addDate(d, (3 - wk));
				this.butday3 = this.getFormatDatemmdd(dd);
				this.butvallist.push(this.getFormatDate(dd));
				dd = this.addDate(d, (4 - wk));
				this.butday4 = this.getFormatDatemmdd(dd);
				this.butvallist.push(this.getFormatDate(dd));
				dd = this.addDate(d, (5 - wk));
				this.butday5 = this.getFormatDatemmdd(dd);
				this.butvallist.push(this.getFormatDate(dd));
				dd = this.addDate(d, (6 - wk));
				this.butday6 = this.getFormatDatemmdd(dd);
				this.butvallist.push(this.getFormatDate(dd));
				dd = this.addDate(d, (7 - wk));
				this.butday7 = this.getFormatDatemmdd(dd);
				this.butvallist.push(this.getFormatDate(dd));

			},
			getsxsjdlist() {
				this.jssjdlist = [];
				var logininfo = JSON.parse(localStorage.logininfo);
				var vtime = this.butvallist[(this.but - 1)];
				var wek = this.but;
				if(wek == 7) {
					wek = 0;
				}
				this.searchLoading = true;
				var params2 = qs.stringify({
					lsid: this.params.lsid,
					time: vtime,
					week: wek,
					xxid: logininfo.xxid,
					yhid: logininfo.id,
				})
				//执行
				this.axios.post(this.$store.state.api + 'bj/lskb', params2)
					.then(res => {
						this.jssjdlist = res.data.data.list
						console.log(this.jssjdlist,8888)
						this.searchLoading = false;
					}).catch(res => {})
			},
			removecl(sjdid) {
				var tempclist = [];

				for(let i in this.cllist) {
					var one = this.cllist[i];
					if(one.id == sjdid) {} else {
						tempclist.push(one);
					}
				}
				this.cllist = tempclist;
			},
			isexistclid(sid) {
				var i = 0;
				for(var a in this.cllist) {
					var one = this.cllist[a];
					if((one.id + 0) == (sid + 0)) {
						i = 1
						break;
					}
				}
				if(i == 0) {
					return false;
				} else {
					return true;
				}
			},
			addcl() {
				var weeklist = ['周日', '周一', '周二', '周三', '周四', '周五', '周六', '周日']
				var week1 = weeklist[this.but] + "";
				for(let i in this.jssjdlist) {
					if(this.jssjdlist[i].chk1 == true && !this.isexistclid(this.jssjdlist[i].sjdid1)) {
						var aa = {
							weektext: week1,
							jsid: this.jssjdlist[i].jsid,
							id: this.jssjdlist[i].sjdid1,
							sjd1: this.jssjdlist[i].sjd1.substring(0, 2),
							sjd2: this.jssjdlist[i].sjd1.substring(3, 5),
							sjd3: this.jssjdlist[i].sjd1.substring(7, 9),
							sjd4: this.jssjdlist[i].sjd1.substring(10, 12),
							jsmc: this.jssjdlist[i].jsmc,
							bjtime: this.jssjdlist[i].bjtime,
							week: this.jssjdlist[i].week
						};
						this.cllist.push(aa);
					}
					
					if(this.jssjdlist[i].chk2 == true && !this.isexistclid(this.jssjdlist[i].sjdid2)) {
						var aa = {
							weektext: week1,
							jsid: this.jssjdlist[i].jsid,
							id: this.jssjdlist[i].sjdid2,
							sjd1: this.jssjdlist[i].sjd2.substring(0, 2),
							sjd2: this.jssjdlist[i].sjd2.substring(3, 5),
							sjd3: this.jssjdlist[i].sjd2.substring(7, 9),
							sjd4: this.jssjdlist[i].sjd2.substring(10, 12),
							jsmc: this.jssjdlist[i].jsmc,
							bjtime: this.jssjdlist[i].bjtime,
							week: this.jssjdlist[i].week
						};
						this.cllist.push(aa);
					}
					if(this.jssjdlist[i].chk3 == true && !this.isexistclid(this.jssjdlist[i].sjdid3)) {
						var aa = {
							weektext: week1,
							jsid: this.jssjdlist[i].jsid,
							id: this.jssjdlist[i].sjdid3,
							sjd1: this.jssjdlist[i].sjd3.substring(0, 2),
							sjd2: this.jssjdlist[i].sjd3.substring(3, 5),
							sjd3: this.jssjdlist[i].sjd3.substring(7, 9),
							sjd4: this.jssjdlist[i].sjd3.substring(10, 12),
							jsmc: this.jssjdlist[i].jsmc,
							bjtime: this.jssjdlist[i].bjtime,
							week: this.jssjdlist[i].week
						};
						this.cllist.push(aa);
					}
					if(this.jssjdlist[i].chk4 == true && !this.isexistclid(this.jssjdlist[i].sjdid4)) {
						var aa = {
							weektext: week1,
							jsid: this.jssjdlist[i].jsid,
							id: this.jssjdlist[i].sjdid4,
							sjd1: this.jssjdlist[i].sjd4.substring(0, 2),
							sjd2: this.jssjdlist[i].sjd4.substring(3, 5),
							sjd3: this.jssjdlist[i].sjd4.substring(7, 9),
							sjd4: this.jssjdlist[i].sjd4.substring(10, 12),
							jsmc: this.jssjdlist[i].jsmc,
							bjtime: this.jssjdlist[i].bjtime,
							week: this.jssjdlist[i].week
						};
						this.cllist.push(aa);
					}
					if(this.jssjdlist[i].chk5 == true && !this.isexistclid(this.jssjdlist[i].sjdid5)) {
						var aa = {
							weektext: week1,
							jsid: this.jssjdlist[i].jsid,
							id: this.jssjdlist[i].sjdid5,
							sjd1: this.jssjdlist[i].sjd5.substring(0, 2),
							sjd2: this.jssjdlist[i].sjd5.substring(3, 5),
							sjd3: this.jssjdlist[i].sjd5.substring(7, 9),
							sjd4: this.jssjdlist[i].sjd5.substring(10, 12),
							jsmc: this.jssjdlist[i].jsmc,
							bjtime: this.jssjdlist[i].bjtime,
							week: this.jssjdlist[i].week
						};
						this.cllist.push(aa);
					}
					if(this.jssjdlist[i].chk6 == true && !this.isexistclid(this.jssjdlist[i].sjdid6)) {
						var aa = {
							weektext: week1,
							jsid: this.jssjdlist[i].jsid,
							id: this.jssjdlist[i].sjdid6,
							sjd1: this.jssjdlist[i].sjd6.substring(0, 2),
							sjd2: this.jssjdlist[i].sjd6.substring(3, 5),
							sjd3: this.jssjdlist[i].sjd6.substring(7, 9),
							sjd4: this.jssjdlist[i].sjd6.substring(10, 12),
							jsmc: this.jssjdlist[i].jsmc,
							bjtime: this.jssjdlist[i].bjtime,
							week: this.jssjdlist[i].week
						};
						this.cllist.push(aa);
					}

				}
				console.log(this.cllist,11122)
			},
			bccl(isqz) {
				var restr = "";
				for(let i in this.cllist) {
					var onerow = this.cllist[i];
					var sjd = onerow.sjd1 + ":" + onerow.sjd2 + "--" + onerow.sjd3 + ":" + onerow.sjd4;
					var onerow = onerow.jsid + "," + onerow.week + "," + onerow.bjtime + "," + sjd + "," + onerow.id + ";";

					restr = restr + onerow;
				}
				if(restr == ''){
					this.$message.warning('请选择时间段');
					return
				}
				var logininfo = JSON.parse(localStorage.logininfo);
				var params2 = qs.stringify({
					iscx: this.iscx == true ? 'Y':'N',
					isqz: isqz,
					xxid: logininfo.xxid + '',
					yhid: logininfo.id + '',
					bjid: this.params.id + '',
					lsid: this.form.lsid,
					sjdids: restr + "",
					day: this.vday //排课开始day
				})
				this.axios.post(this.$store.state.api + 'bj/insertcl', params2)
					.then(res => {
//						console.log(res, 876)
						if(res.data.code == 1) {
							if(isqz == "Y"){
								this.$emit('close', {
									type: 'edit',
									cancel: false
								});
								this.$message.success("保存成功!!");
							}else{
								this.ckct()
							}
						} else {
							if(isqz == "N"){
								this.ckct()
							}else{
								this.$message.warning(res.data.msg);
							}
						}
					}).catch(res => {
						this.$message.warning("服务器异常");
					})

			},
			getcomboxlist() {
				var logininfo = JSON.parse(localStorage.logininfo);
				this.searchLoading = true;
				var params2 = qs.stringify({
					yhid: logininfo.id,
					xxid: logininfo.xxid
				})
				//执行
				this.axios.post(this.$store.state.api + 'ls/getlslist', params2)
					.then(res => {
						let data = res.data.data;
						let list = data.list;
						this.comboxlist = list;
					}).catch(res => {
						this.searchLoading = false;
					})
			},
			cancel() {
				this.$emit('close', {
					type: 'edit',
					cancel: true
				})
			},
			/* 组件的弹框  */
			componentsShow(type, data) { //显示组件
				for(let i in this.portComponents) {
					if(type == this.portComponents[i].type) {
						this.portComponents[i].toggle = true;
						this.portComponents[i].params = data;
						if(this.portComponents[i].data.type == 'single') {
							this.portComponents[i].data.index = data.row.id
						} else if(this.portComponents[i].data.type == 'multiple') {
							this.portComponents[i].data.index = this.multipleDelNum;
						} else {
							return;
						}
						return;
					}
				}
			},
			componentsHide(setting) { //关闭组件
				for(let i in this.portComponents) {
					if(setting.type == this.portComponents[i].type) {
						this.portComponents[i].toggle = false;
						if(!setting.cancel) {
							this.search();
						}
						return;
					}
				}
			}
		}
	}
</script>
<style lang="less" scoped>
	.wrap {
		.tab {
			width: 93%;
			margin: 0 auto;
			border: 1px solid #D8DCE5;
			margin-top: 10px;
			tr {
				border: 1px solid #D8DCE5;
			}
			td {
				text-align: center;
				border: 1px solid #D8DCE5;
				div {
					height: 50%;
					line-height: 50px;
					border: 1px solid #D8DCE5;
				}
			}
		}
		.ullist {
			overflow: hidden;
			width: 100%;
			margin: 0 auto;
			text-align: center;
		}
		.row {
			height: 50px;
			line-height: 50px;
		}
		.lis{
			margin-left: 5%;
			margin-top: 2px;
			height: 50px;
			width: 80%;
			line-height: 50px;
			border: 1px solid #4D6B8A;
			.sp1{
				text-align: center;
				width: 5%;
				float: left;
			}
		}
		.bt {
			position: absolute;
			width: 100%;
			height: 50px;
			margin: 0 auto;
			top: 0;
			left: 0;
			// margin-top: -70px;
			background: #50BFFF;
			// margin-left: -20px;
			// margin-bottom: 20px;
			color: white;
			text-align: left;
			line-height: 50px;
			font-size: 17px;
			padding-left: 15px;
		}
		.close {
			z-index: 10;
			position: absolute;
		}
		.input-block {
			margin-bottom: 20px;
			text-align: center;
		}
		.input {
			width: 280px;
			display: inline-block;
			margin-left: 10px;
		}
		.input1 {
			width: 210px;
			display: inline-block;
			margin-left: 10px;
		}
		.input-block span {
			display: inline-block;
			width: 70px;
			text-align: right;
		}
		.submit {
			width: 120px;
			display: block;
			margin: 0 auto;
		}
		.wpfl {
			background: #fff;
			position: absolute;
			top: 5px;
			left: 5px;
		}
		.wpfl1 {
			width: 100%;
			background: #fff;
			position: absolute;
			top: 5px;
			left: -66px;
		}
		.lskb {
			width: 140%;
			background: #fff;
			position: absolute;
			top: -100px;
			left: -20%;
			z-index: 50;
			padding-bottom: 100px;
		}
		.form {
			text-align: left;
			margin-top: 10px;
			.zzz {
				overflow: hidden;
			}
			.select-block {
				width: 85%;
				display: block;
			}
			.select-block1 {
				width: 63%;
				// display: block;
			}
			.select-block3 {
				width: 50%;
				// display: block;
			}
			.zjselect-block {
				width: 20%;
				float: left;
				display: block;
			}
			.el-form-item {
				margin-bottom: 10px;
			}
			.weekdiv {
				width: 100%;
				height: 100%;
				margin: 0 auto;
				background: lightblue;
			}
			.wdiv0 {
				line-height: 45px;
				width: 110px;
				float: left;
				background: lightgoldenrodyellow;
			}
			.wdiv1 {
				width: 50px;
				float: left;
				background: lightgoldenrodyellow;
			}
			.wdiv2 {
				width: 50px;
				float: left;
				background: lightgoldenrodyellow;
			}
			.wdiv3 {
				width: 50px;
				float: left;
				background: lightgoldenrodyellow;
			}
			.wdiv4 {
				width: 50px;
				float: left;
				background: lightgoldenrodyellow;
			}
			.wdiv5 {
				width: 50px;
				float: left;
				background: lightgoldenrodyellow;
			}
			.wdiv6 {
				width: 50px;
				float: left;
				background: lightgoldenrodyellow;
			}
			.wdiv7 {
				width: 50px;
				float: left;
				background: lightgoldenrodyellow;
			}
			.el-checkbox+.el-checkbox {
				margin-left: 0px;
			}
			.el-button+.el-button {
				margin-left: 0px;
			}
		}
	}
</style>